package de.tabreaderandroid.tabreader.activities;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import de.tabreaderandroid.tabreader.R;
import de.tabreaderandroid.tabreader.model.TabListAdapter;
import de.tabreaderandroid.tabreader.model.TabListItem;
import de.tabreaderandroid.tabreader.preferences.FolderRepository;
import de.tabreaderandroid.tabreader.utils.TabFileUtils;

/**
 * Activity listing all available tabs from the set tab folder.
 * 
 * @author André Stannek
 * 
 */
public class TabListActivity extends Activity {

	private final FolderRepository folderRepository;

	/**
	 * 
	 */
	public TabListActivity() {
		super();
		folderRepository = new FolderRepository(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_list);
		ListView list = (ListView) findViewById(R.id.tab_list);
		list.setAdapter(new TabListAdapter(this));
		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				TabListItem tabFile = (TabListItem) parent
						.getItemAtPosition(position);
				Intent showTabIntent = new Intent(TabListActivity.this,
						TabViewActivity.class);
				showTabIntent.putExtra("filePath", tabFile.getFile().getPath());
				TabListActivity.this.startActivity(showTabIntent);
			}
		});
		fillTabList();
	}

	private void fillTabList() {
		List<String> folders = folderRepository.getAllFolders();
		List<TabListItem> tabFiles = new ArrayList<TabListItem>();
		if (folders.isEmpty()) {
			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle(getResources().getString(
					R.string.startup_error));
			alertDialog.setMessage(getResources().getString(
					R.string.no_folder_error));
			alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							openFolderSettings();
						}
					});
			alertDialog.show();
		} else {
			for (String folderPath : folders) {
				File folder = new File(URI.create("file:".concat(folderPath)));
				tabFiles.addAll(TabFileUtils
						.getTabFilesFromFolder(folder, true));
			}
		}
		ListView list = (ListView) findViewById(R.id.tab_list);
		TabListAdapter adapter = (TabListAdapter) list.getAdapter();
		adapter.clear();
		adapter.addAll(tabFiles);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.tab_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_folders:
			openFolderSettings();
			break;
		case R.id.action_settings:
			Intent prefsIntent = new Intent(TabListActivity.this,
					SetPreferencesActivity.class);
			TabListActivity.this.startActivity(prefsIntent);
			break;
		default:
			return false;
		}
		return true;
	}

	private void openFolderSettings() {
		Intent selectFolderIntent = new Intent(TabListActivity.this,
				SelectFoldersActivity.class);
		TabListActivity.this.startActivity(selectFolderIntent);
	}
}
